package br.com.bancodecurriculos.pessoa.fisica.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import br.com.bancodecurriculos.pessoa.comum.dominio.Email;
import br.com.bancodecurriculos.pessoa.comum.dominio.Endereco;
import br.com.bancodecurriculos.pessoa.comum.dominio.Telefone;
import br.com.bancodecurriculos.pessoa.fisica.dominio.PessoaFisica.EstadoCivil;
import br.com.bancodecurriculos.util.gui.Form;

public class PessoaFisicaForm extends Form {

	private static final long serialVersionUID = 1L;

	private static final String CPF = "CPF *";
	private static final String MASCARA_CPF = "###.###.###-##";
	private static final String NOME = "Nome *";
	private static final String SEXO = "Sexo *";
	private static final String MASCULINO = "M";
	private static final String FEMININO = "F";
	private static final String DATA_NASCIMENTO = "Data de Nascimento *";
	private static final String MASCARA_DATA_NASCIMENTO = "##/##/####";
	private static final String ESTADO_CIVIL = "Estado Civil *";
	private static final String ENDERECOS = "Endere\u00E7os";
	private static final String TELEFONES = "Telefones";
	private static final String EMAILS = "Emails";
	private static final String SALVAR = "Salvar";
	private static final String PROXIMO = ">>";

	private JLabel lblCpf;
	private JLabel lblNome;
	private JLabel lblSexo;
	private JLabel lblDataDe;
	private JTextField txtFNome;
	private JLabel lblEstadoCivil;
	private JFormattedTextField frmtdtxtfldCPF;
	private JFormattedTextField frmtdtxtfldDataNascimento;
	private JRadioButton rdbtnFeminino;
	private JRadioButton rdbtnMasculino;
	private JComboBox<String> cmbbxEstadoCivil;
	private ButtonGroup radios;
	private JButton btnTelefones;
	private JButton btnEmails;
	private JButton btnSalvar;
	private JButton btnProximo;
	private JButton btnEndereos;

	private List<Endereco> enderecos;
	private List<Email> emails;
	private List<Telefone> telefones;

	public PessoaFisicaForm() {
		super();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100, 100, 470, 305);
		this.getContentPane().setLayout(null);
		try {
			this.initComponentes();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		this.initListas();
		this.carregarEstadosCivis();
		this.acoes();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	private void initListas() {
		this.enderecos = new ArrayList<Endereco>();
		this.emails = new ArrayList<Email>();
		this.telefones = new ArrayList<Telefone>();
	}

	private void initComponentes() throws ParseException {
		this.lblCpf = new JLabel(CPF);
		this.lblCpf.setFont(super.getFont());
		this.lblCpf.setBounds(10, 11, 151, 14);
		this.getContentPane().add(this.lblCpf);

		this.frmtdtxtfldCPF = new JFormattedTextField(new MaskFormatter(
				MASCARA_CPF));
		this.frmtdtxtfldCPF.setFont(super.getFont());
		this.frmtdtxtfldCPF.setBounds(173, 8, 119, 20);
		this.getContentPane().add(this.frmtdtxtfldCPF);

		this.lblNome = new JLabel(NOME);
		this.lblNome.setFont(super.getFont());
		this.lblNome.setBounds(10, 36, 151, 14);
		this.getContentPane().add(this.lblNome);

		this.txtFNome = new JTextField();
		this.txtFNome.setFont(super.getFont());
		this.txtFNome.setBounds(173, 33, 283, 20);
		this.getContentPane().add(this.txtFNome);
		this.txtFNome.setColumns(10);

		this.lblSexo = new JLabel(SEXO);
		this.lblSexo.setFont(super.getFont());
		this.lblSexo.setBounds(10, 61, 151, 14);
		this.getContentPane().add(this.lblSexo);

		this.rdbtnFeminino = new JRadioButton(FEMININO);
		this.rdbtnFeminino.setFont(super.getFont());
		this.rdbtnFeminino.setBounds(173, 57, 33, 23);
		this.getContentPane().add(this.rdbtnFeminino);

		this.rdbtnMasculino = new JRadioButton(MASCULINO);
		this.rdbtnMasculino.setFont(super.getFont());
		this.rdbtnMasculino.setBounds(210, 57, 40, 23);
		this.getContentPane().add(this.rdbtnMasculino);

		this.radios = new ButtonGroup();
		this.radios.add(rdbtnFeminino);
		this.radios.add(rdbtnMasculino);

		this.lblDataDe = new JLabel(DATA_NASCIMENTO);
		this.lblDataDe.setFont(super.getFont());
		this.lblDataDe.setBounds(10, 86, 151, 14);
		this.getContentPane().add(this.lblDataDe);

		this.frmtdtxtfldDataNascimento = new JFormattedTextField(
				new MaskFormatter(MASCARA_DATA_NASCIMENTO));
		this.frmtdtxtfldDataNascimento.setFont(super.getFont());
		this.frmtdtxtfldDataNascimento.setBounds(173, 83, 89, 20);
		this.getContentPane().add(this.frmtdtxtfldDataNascimento);

		this.lblEstadoCivil = new JLabel(ESTADO_CIVIL);
		this.lblEstadoCivil.setFont(super.getFont());
		this.lblEstadoCivil.setBounds(10, 112, 151, 14);
		this.getContentPane().add(this.lblEstadoCivil);

		this.cmbbxEstadoCivil = new JComboBox<String>();
		this.cmbbxEstadoCivil.setFont(super.getFont());
		this.cmbbxEstadoCivil.setBounds(173, 109, 113, 20);
		this.getContentPane().add(this.cmbbxEstadoCivil);

		this.btnEndereos = new JButton(ENDERECOS);
		this.btnEndereos.setFont(super.getFont());
		this.btnEndereos.setBounds(180, 141, 113, 23);
		this.getContentPane().add(this.btnEndereos);

		this.btnTelefones = new JButton(TELEFONES);
		this.btnTelefones.setFont(super.getFont());
		this.btnTelefones.setBounds(180, 176, 113, 23);
		this.getContentPane().add(this.btnTelefones);

		this.btnEmails = new JButton(EMAILS);
		this.btnEmails.setFont(super.getFont());
		this.btnEmails.setBounds(180, 211, 113, 23);
		this.getContentPane().add(this.btnEmails);

		this.btnSalvar = new JButton(SALVAR);
		this.btnSalvar.setFont(super.getFont());
		this.btnSalvar.setBounds(198, 246, 75, 23);
		this.getContentPane().add(this.btnSalvar);

		this.btnProximo = new JButton(PROXIMO);
		this.btnProximo.setFont(super.getFont());
		this.btnProximo.setBounds(398, 246, 58, 23);
		this.getContentPane().add(this.btnProximo);
	}

	private void carregarEstadosCivis() {
		EstadoCivil[] civils = EstadoCivil.values();
		for (EstadoCivil civil : civils) {
			this.cmbbxEstadoCivil.addItem(civil.getNome());
		}
	}

	private void acoes() {
		this.btnEndereos.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				EnderecoForm form = new EnderecoForm();
				form.setModal(true);
				enderecos = form.showDialog();
			}
		});

		this.btnEmails.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				EmailForm form = new EmailForm();
				form.setModal(true);
				emails = form.showDialog();
			}
		});
	}

}
